package com.qckj.amz.controller;

import com.qckj.amz.domain.ShopAuth;
import com.qckj.amz.service.IConfigMarketplaceService;
import com.qckj.amz.service.IShopAuthService;
import com.qckj.common.annotation.Log;
import com.qckj.common.core.controller.BaseController;
import com.qckj.common.core.domain.AjaxResult;
import com.qckj.common.core.page.TableDataInfo;
import com.qckj.common.enums.BusinessType;
import com.qckj.common.utils.SecurityUtils;
import com.qckj.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 店铺授权Controller
 *
 * @author luchuan
 * @since 1.0 2021-11-09
 */
@RestController
@RequestMapping("/amz/shopAuth")
public class ShopAuthController extends BaseController {
    @Autowired
    private IShopAuthService shopAuthService;
    @Autowired
    private IConfigMarketplaceService configMarketplaceService;

    /**
     * 查询店铺授权列表
     */
    @PreAuthorize("@ss.hasPermi('amz:shopAuth:list')")
    @GetMapping("/list")
    public TableDataInfo list(ShopAuth shopAuth) {
        TableDataInfo data = shopAuthService.getShopAuthPage(shopAuth);
        return data;
    }

    /**
     * 导出店铺授权列表
     */
    @PreAuthorize("@ss.hasPermi('amz:shopAuth:export')")
    @Log(title = "店铺授权", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ShopAuth shopAuth) {
        List<ShopAuth> list = shopAuthService.findShopAuthList(shopAuth);
        ExcelUtil<ShopAuth> util = new ExcelUtil<>(ShopAuth.class);
        return util.exportExcel(list, "shopAuth");
    }

    /**
     * 获取店铺授权详细信息
     */
    @PreAuthorize("@ss.hasPermi('amz:shopAuth:query')")
    @GetMapping(value = "/{shopAuthId}")
    public AjaxResult getInfo(@PathVariable("shopAuthId") Long shopAuthId) {
        return AjaxResult.success(shopAuthService.findById(shopAuthId));
    }

    /**
     * 新增店铺授权
     * 同时检查是否已授权
     */
    @PreAuthorize("@ss.hasPermi('amz:shopAuth:add')")
    @Log(title = "店铺授权", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShopAuth shopAuth) {
        AjaxResult ajaxResult = shopAuthService.save(shopAuth);
        return ajaxResult;
    }

    /**
     * 修改店铺授权
     */
    @PreAuthorize("@ss.hasPermi('amz:shopAuth:edit')")
    @Log(title = "店铺授权", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShopAuth shopAuth) {
        AjaxResult ajaxResult = shopAuthService.edit(shopAuth);
        return ajaxResult;
    }

    /**
     * 删除店铺授权
     */
    @PreAuthorize("@ss.hasPermi('amz:shopAuth:remove')")
    @Log(title = "店铺授权", businessType = BusinessType.DELETE)
	@DeleteMapping("/{shopAuthIds}")
    public AjaxResult remove(@PathVariable Long[] shopAuthIds) {
        shopAuthService.deleteByIds(Arrays.asList(shopAuthIds));
        return AjaxResult.success();
    }

    /**
     * 店铺授权/新增-通过当前登录人获取已维护的店铺
     * @return
     */
    @RequestMapping("/getShopCodeByUsername")
    @ResponseBody
    public AjaxResult getShopCodeByUsername() {
        AjaxResult ajaxResult = shopAuthService.getShopCodeByUsername();
        return ajaxResult;
    }

    /**
     * 上传产品/选择店铺-通过店铺代码获取已授权的站点
     * @return
     */
    @RequestMapping("/getCountryCodeByShopCode")
    @ResponseBody
    public AjaxResult getCountryCodeByShopCode(@RequestParam("shopCode") String shopCode) {
        AjaxResult ajaxResult = shopAuthService.getCountryCodeByShopCode(shopCode);
        return ajaxResult;
    }
    /**
     * 通过当前登录人获取自己授权的多个店铺信息
     * @return
     */
    @RequestMapping("/getShopCodeToShopAuth")
    @ResponseBody
    public AjaxResult getShopCodeToShopAuth() {
        AjaxResult ajaxResult = shopAuthService.getShopCodeToShopAuth();
        return ajaxResult;
    }

    /**
     * 通过区域代码获取spapi授权地址
     */
    @PreAuthorize("@ss.hasPermi('amz:shopAuth:auth')")
    @GetMapping("/getSpapiAuthUrl/{sellingRegionCode}")
    public AjaxResult getSpapiAuthUrl(@PathVariable("sellingRegionCode") String sellingRegionCode) {
        AjaxResult ajaxResult= configMarketplaceService.getSpapiAuthUrl(sellingRegionCode);
        return ajaxResult;
    }

    /**
     * 执行授权
     */
    @PreAuthorize("@ss.hasPermi('amz:shopAuth:auth')")
    @PostMapping("/executeAuth")
    public AjaxResult executeAuth(String formAuth) {
        AjaxResult ajaxResult= shopAuthService.executeAuth(formAuth);
        return ajaxResult;
    }


    /**
     * 店铺授权/订单同步
     */
    @PreAuthorize("@ss.hasPermi('amz:shopAuth:sync')")
    @Log(title = "店铺授权/订单同步", businessType = BusinessType.SYNC)
    @RequestMapping("syncOrder/{shopAuthIds}")
    public AjaxResult syncOrder(@PathVariable Long[] shopAuthIds) {
        AjaxResult ajaxResult= shopAuthService.syncOrder(shopAuthIds);
        return ajaxResult;
    }



}
